'use strict';

angular.module('myapp', ['ngResource', 'ng-chrome-cast-channels']);

angular.module('myapp').factory('Content', ['$resource', function ($resource) {

    return $resource('/api/contents/:id', {
        id: '@_id'
    }, {
        'update': {
            method: 'PUT'
        }
    });

}]);

var appId = '72AF9B5E';
var messageNS = 'urn:x-cast:com.google.cast.sample.helloworld';


angular.module('myapp').controller('runCtrl', ['$scope', 'Content', '$timeout', 'ChromeCastReceiverChannel', function ($scope, Content, $timeout, ChromeCastReceiverChannel) {

    $scope.callback = function(data){
        //$scope.data = data;
        //$scope.$apply();
    };

    ChromeCastReceiverChannel.initialize(messageNS, $scope.callback)

    $scope.contents = Content.query(function () {

        next(-1);
    });

    $scope.isFlash = function(content) {
        return content.name.indexOf('.swf') != -1;
    }

    $scope.isNotFlash = function(content) {
        return !$scope.isFlash(content);
    }

    var next = function (index) {

        index = (index+ 1)%$scope.contents.length;

        $scope.current = $scope.contents[index];

        if(!$scope.current.enable) {
            next(index);
            return;
        }

        $timeout(function () {

            next(index);

        }, $scope.current.duration * 1000);
    }


}]);
